﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class ProcessBiz
    {
        public static List<Process> GetProcessListByPageAndDefID(int page, int pageSize, int defID, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                     
                    var expression = from c in ctx.Process
                                     join u in ctx.User
                                     on c.SignUsers equals u.UserName
                                     where c.ProcessDefID==defID && c.Status !=(int)ItemStatus.Delete
                                     orderby c.ID
                                     select new{c,u.UserName,u.RealName};
                    
                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Process().Set(p.c).LoadUser(new U_User() {UserName=p.UserName,RealName=p.RealName })).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static List<Process> GetProcessListByDefID(int defID,string key="")
        {
            try
            {
                
                var processList = new Process().FindAll(c => c.ProcessDefID == defID && c.Status == (int)ItemStatus.Enable&&c.ProcessName.Contains(key));
                return processList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static Process AddProcess(int processDefID,string processName,int signType,string signUsers ,int changeState)
        {
            try
            {
                var process = new Process()
                {
                    ChangeState=changeState,
                    ProcessDefID=processDefID,
                    ProcessName=processName,
                    SignType=signType,
                    SignUsers=signUsers,
                    Status=ItemStatus.Enable
                };

                process = process.PostAdd();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static Process EditProcess(int processID, int processDefID, string processName, int signType, string signUsers, int changeState)
        {
            try
            {
                var process = new Process().Find(processID.ToString());
                process.ChangeState = changeState;
                process.ProcessDefID=processDefID;
                process.ProcessName = processName;
                process.SignType = signType;
                process.SignUsers = signUsers;
                process = process.PostModify();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static Process DeleteProcess(int processID)
        {
            try
            {
                var process = new Process().Find(processID.ToString());
                process.Status = ItemStatus.Delete;
                process = process.PostModify();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static Process ChangeStatus(int processID, int status)
        {
            try
            {
                var process = new Process().Find(processID.ToString());
                process.Status = (ItemStatus)status;
                process = process.PostModify();
                return process;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static bool ProcessExsit(string processName, int processID = 0)
        {
            bool isTrue = false;
            try
            {
                var process = new Process().Find(c => c.ProcessName == processName && c.Status != (int)ItemStatus.Delete && c.ID != processID);
                if (process != null)
                {
                    isTrue = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return isTrue;
        }
    }
}
